package com.fangrui.service.impl;

import com.fangrui.dto.AccountDto;
import com.fangrui.entity.DormitoryAdmin;
import com.fangrui.entity.SystemAdmin;
import com.fangrui.form.AccountForm;
import com.fangrui.mapper.DormitoryAdminMapper;
import com.fangrui.mapper.DormitoryMapper;
import com.fangrui.mapper.SystemAdminMapper;
import com.fangrui.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/21.
//
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private SystemAdminMapper systemAdminMapper;
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;
    @Override
    public AccountDto login(AccountForm accountForm) {
        AccountDto dto = new AccountDto<>();
        System.out.println(accountForm.toString());
        switch(accountForm.getType()){
            case "systemAdmin":
                SystemAdmin systemAdmin = this.systemAdminMapper.findByUsername(accountForm.getUsername());
                if(systemAdmin == null){
                    dto.setCode(-1);
                }else if(!systemAdmin.getPassword().equals(accountForm.getPassword())){
                    dto.setCode(-2);
                }else {
                    dto.setCode(0);
                    dto.setAdmin(systemAdmin);
                }
                break;
            case "dormitoryAdmin":
                DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.findByUserName(accountForm.getUsername());
                if(dormitoryAdmin == null){
                    dto.setCode(-1);
                } else if (!dormitoryAdmin.getPassword().equals(accountForm.getPassword())) {
                    dto.setCode(-2);
                }else {
                    dto.setCode(0);
                    dto.setAdmin(dormitoryAdmin);
                }
                break;
        }
        return dto;
    }
}
